package com.zl;

/**
 * geek
 * 2015/5/14
 * 顺序表。数组实现
 */

class DATA {
    String key;
    String name;
    int age;

    public DATA() {
    }

    public DATA(String key, String name, int age) {
        this.key = key;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "DATA{" +
                "key='" + key + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class SLType {
    static final int maxlen = 100;
    DATA[] listData;
    int len;

    void init() {
        this.len = 0;
        this.listData = new DATA[maxlen + 1];
    }


    boolean is_full() {
        return this.len == maxlen;
    }

    boolean is_empty() {
        return this.len == 0;
    }


    int listLen( ) {
        return this.len;
    }

    boolean insert(int n, DATA data) {
        if (is_full()) {
            System.out.println("顺序表已经满了");
            return false;
        }

        if (n <= 0 || n > this.len + 1 || n >= maxlen) {
            System.out.println("插入元素位置错误");
            return false;
        }


        for (int i = this.len - 1; i >= n - 1; i--) {
            this.listData[i + 1] = this.listData[i];
        }

        this.listData[n - 1] = data;
        this.len++;
        return true;
    }

    boolean append(DATA data) {
        if (is_full()) {
            return false;
        }

        this.listData[this.len] = data;
        this.len++;
        return true;
    }


    boolean delete(int n, DATA data) {
        if (is_empty()) {
            return false;
        }

        if (n <= 0 || n > this.len) {
            System.out.println("删除元素位置错误");
            return false;
        }

        for (int i = n; i < this.len; i++) {
            this.listData[i - 1] = this.listData[i];
        }

        this.len--;
        return true;

    }

    void showAll() {
        for (int i = 0; i < this.len; i++) {
            System.out.println(this.listData[i]);
        }
    }


}

public class SequenceList {
    public static void main(String[] args) {
        SLType slType = new SLType();
        slType.init();

        DATA d1 = new DATA("01", "jack", 17);
        DATA d2 = new DATA("02", "peter", 38);
        DATA d3 = new DATA("03", "lily", 55);
        DATA d4 = new DATA("100", "haml", 00);

        slType.append(d1);
        slType.append(d2);
        slType.append(d3);
        System.out.println("append after");
        slType.showAll();

        System.out.println("insert after");

        slType.insert(2, d4);
        slType.showAll();

        System.out.println("delete after");
        DATA temp = new DATA();
        slType.delete(2,temp);
        slType.showAll();
    }
}
